# This file sets up a CMakeCache for a Fuchsia toolchain build.

set(LLVM_TARGETS_TO_BUILD Native CACHE STRING "") # X86;ARM;AArch64;RISCV

set(PACKAGE_VENDOR OWenT CACHE STRING "")

set(LLVM_ENABLE_PROJECTS "bolt;clang;clang-tools-extra;lld;llvm;lldb;libclc;polly;pstl" CACHE STRING "")

set(LLVM_ENABLE_DIA_SDK OFF CACHE BOOL "")
# set(LLVM_ENABLE_LIBEDIT OFF CACHE BOOL "")
set(LLVM_ENABLE_LIBXML2 OFF CACHE BOOL "")
set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR ON CACHE BOOL "")
set(LLVM_ENABLE_TERMINFO OFF CACHE BOOL "")
set(LLVM_ENABLE_UNWIND_TABLES OFF CACHE BOOL "")
set(LLVM_ENABLE_Z3_SOLVER OFF CACHE BOOL "")
set(LLVM_ENABLE_ZLIB OFF CACHE BOOL "")
set(LLVM_INCLUDE_DOCS OFF CACHE BOOL "")
set(LLVM_INCLUDE_EXAMPLES OFF CACHE BOOL "")
set(LLVM_INCLUDE_GO_TESTS OFF CACHE BOOL "")
#[[
# https://github.com/llvm/llvm-project/issues/57545
# ld.gold: internal error in do_layout
]]
set(LLVM_LLVM_INCLUDE_TESTS OFF CACHE BOOL "")
set(BOLT_INCLUDE_TESTS OFF CACHE BOOL "")

if(WIN32)
  set(LLVM_USE_CRT_RELEASE "MT" CACHE STRING "")
endif()

set(CLANG_DEFAULT_CXX_STDLIB libc++ CACHE STRING "")
set(CLANG_DEFAULT_LINKER lld CACHE STRING "")
set(CLANG_DEFAULT_OBJCOPY llvm-objcopy CACHE STRING "")
set(CLANG_DEFAULT_RTLIB compiler-rt CACHE STRING "")
set(CLANG_DEFAULT_UNWINDLIB libunwind CACHE STRING "")
set(CLANG_ENABLE_ARCMT OFF CACHE BOOL "")
set(CLANG_ENABLE_STATIC_ANALYZER OFF CACHE BOOL "")
set(CLANG_PLUGIN_SUPPORT OFF CACHE BOOL "")

set(ENABLE_LINKER_BUILD_ID ON CACHE BOOL "")
set(ENABLE_X86_RELAX_RELOCATIONS ON CACHE BOOL "")

set(LLVM_ENABLE_ASSERTIONS ON CACHE BOOL "")
set(LLVM_ENABLE_BACKTRACES ON CACHE BOOL "")
set(CMAKE_BUILD_TYPE Release CACHE STRING "")
if(APPLE)
  set(CMAKE_OSX_DEPLOYMENT_TARGET "10.13" CACHE STRING "")
elseif(WIN32)
  set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded" CACHE STRING "")
endif()

if(APPLE)
  set(COMPILER_RT_ENABLE_IOS OFF CACHE BOOL "")
  set(COMPILER_RT_ENABLE_TVOS OFF CACHE BOOL "")
  set(COMPILER_RT_ENABLE_WATCHOS OFF CACHE BOOL "")
endif()

if(WIN32)
  set(LIBCXX_ABI_VERSION 2 CACHE STRING "")
  set(LIBCXX_ENABLE_FILESYSTEM OFF CACHE BOOL "")
  set(LIBCXX_ENABLE_ABI_LINKER_SCRIPT OFF CACHE BOOL "")
  set(LIBCXX_ENABLE_SHARED OFF CACHE BOOL "")
  set(BUILTINS_CMAKE_ARGS -DCMAKE_SYSTEM_NAME=Windows CACHE STRING "")
  set(RUNTIMES_CMAKE_ARGS -DCMAKE_SYSTEM_NAME=Windows CACHE STRING "")
  set(LLVM_ENABLE_RUNTIMES "compiler-rt;libcxx" CACHE STRING "")
else()
  set(LIBUNWIND_ENABLE_SHARED OFF CACHE BOOL "")
  set(LIBUNWIND_INSTALL_LIBRARY OFF CACHE BOOL "")
  set(LIBUNWIND_USE_COMPILER_RT ON CACHE BOOL "")
  set(LIBCXXABI_ENABLE_SHARED OFF CACHE BOOL "")
  set(LIBCXXABI_ENABLE_STATIC_UNWINDER ON CACHE BOOL "")
  set(LIBCXXABI_INSTALL_LIBRARY OFF CACHE BOOL "")
  set(LIBCXXABI_USE_COMPILER_RT ON CACHE BOOL "")
  set(LIBCXXABI_USE_LLVM_UNWINDER ON CACHE BOOL "")
  set(LIBCXX_ABI_VERSION 2 CACHE STRING "")
  set(LIBCXX_ENABLE_SHARED OFF CACHE BOOL "")
  set(LIBCXX_ENABLE_STATIC_ABI_LIBRARY ON CACHE BOOL "")
  set(LIBCXX_USE_COMPILER_RT ON CACHE BOOL "")
  set(LLVM_ENABLE_RUNTIMES "compiler-rt;libcxx;libcxxabi;libunwind" CACHE STRING "")
  set(RUNTIMES_CMAKE_ARGS "-DCMAKE_OSX_DEPLOYMENT_TARGET=10.13;-DCMAKE_OSX_ARCHITECTURES=arm64|x86_64" CACHE STRING "")
endif()

if(BOOTSTRAP_CMAKE_SYSTEM_NAME)
  set(target "${BOOTSTRAP_CMAKE_CXX_COMPILER_TARGET}")
  if(STAGE2_LINUX_${target}_SYSROOT)
    set(LLVM_BUILTIN_TARGETS "${target}" CACHE STRING "")
    set(BUILTINS_${target}_CMAKE_SYSTEM_NAME Linux CACHE STRING "")
    set(BUILTINS_${target}_CMAKE_BUILD_TYPE Release CACHE STRING "")
    set(BUILTINS_${target}_CMAKE_SYSROOT ${STAGE2_LINUX_${target}_SYSROOT} CACHE STRING "")

    set(LLVM_RUNTIME_TARGETS "${target}" CACHE STRING "")
    set(RUNTIMES_${target}_CMAKE_SYSTEM_NAME Linux CACHE STRING "")
    set(RUNTIMES_${target}_CMAKE_BUILD_TYPE Release CACHE STRING "")
    set(RUNTIMES_${target}_CMAKE_SYSROOT ${STAGE2_LINUX_${target}_SYSROOT} CACHE STRING "")
    set(RUNTIMES_${target}_COMPILER_RT_USE_BUILTINS_LIBRARY ON CACHE BOOL "")
    set(RUNTIMES_${target}_LIBUNWIND_ENABLE_SHARED OFF CACHE BOOL "")
    set(RUNTIMES_${target}_LIBUNWIND_USE_COMPILER_RT ON CACHE BOOL "")
    set(RUNTIMES_${target}_LIBUNWIND_INSTALL_LIBRARY OFF CACHE BOOL "")
    set(RUNTIMES_${target}_LIBCXXABI_USE_COMPILER_RT ON CACHE BOOL "")
    set(RUNTIMES_${target}_LIBCXXABI_ENABLE_SHARED OFF CACHE BOOL "")
    set(RUNTIMES_${target}_LIBCXXABI_USE_LLVM_UNWINDER ON CACHE BOOL "")
    set(RUNTIMES_${target}_LIBCXXABI_ENABLE_STATIC_UNWINDER ON CACHE BOOL "")
    set(RUNTIMES_${target}_LIBCXXABI_INSTALL_LIBRARY ON CACHE BOOL "")
    set(RUNTIMES_${target}_LIBCXX_USE_COMPILER_RT ON CACHE BOOL "")
    set(RUNTIMES_${target}_LIBCXX_ENABLE_SHARED OFF CACHE BOOL "")
    set(RUNTIMES_${target}_LIBCXX_ENABLE_STATIC_ABI_LIBRARY ON CACHE BOOL "")
    set(RUNTIMES_${target}_LIBCXX_ABI_VERSION 2 CACHE STRING "")
    set(RUNTIMES_${target}_LLVM_ENABLE_ASSERTIONS OFF CACHE BOOL "")
    set(RUNTIMES_${target}_LLVM_ENABLE_RUNTIMES "compiler-rt;libcxx;libcxxabi;libunwind" CACHE STRING "")
    set(RUNTIMES_${target}_SANITIZER_CXX_ABI "libc++" CACHE STRING "")
    set(RUNTIMES_${target}_SANITIZER_CXX_ABI_INTREE ON CACHE BOOL "")
  endif()
endif()

if(UNIX)
  if(BOOTSTRAP_CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN)
    set(STAGE2_APPEND_RPATHS)
    set(STAGE2_RPATH_ORIGIN_EXTERNAL_TOOLCHAIN)
    if(NOT APPLE)
      set(STAGE2_RPATH_ORIGIN "\$ORIGIN")
    else()
      set(STAGE2_RPATH_ORIGIN "@loader_path")
    endif()
    if(CMAKE_INSTALL_PREFIX)
      file(RELATIVE_PATH STAGE2_RPATH_ORIGIN_EXTERNAL_TOOLCHAIN "${CMAKE_INSTALL_PREFIX}/bin"
           "${BOOTSTRAP_CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}")
    endif()
    if(BOOTSTRAP_CMAKE_INSTALL_RPATH)
      set(BOOTSTRAP_CMAKE_INSTALL_RPATH ${BOOTSTRAP_CMAKE_INSTALL_RPATH})
    else()
      # Just like in llvm/cmake/modules/AddLLVM.cmake : llvm_setup_rpath
      set(BOOTSTRAP_CMAKE_INSTALL_RPATH "${STAGE2_RPATH_ORIGIN}/../lib${LLVM_LIBDIR_SUFFIX}")
      if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX))
        list(APPEND BOOTSTRAP_CMAKE_INSTALL_RPATH "${LLVM_LIBRARY_DIR}")
      elseif(LLVM_BUILD_LIBRARY_DIR)
        list(APPEND BOOTSTRAP_CMAKE_INSTALL_RPATH "${LLVM_LIBRARY_DIR}")
      endif()
    endif()
    if(EXISTS "${BOOTSTRAP_CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}/lib64")
      list(APPEND BOOTSTRAP_CMAKE_INSTALL_RPATH "${BOOTSTRAP_CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}/lib64")
      if(STAGE2_RPATH_ORIGIN_EXTERNAL_TOOLCHAIN)
        list(APPEND BOOTSTRAP_CMAKE_INSTALL_RPATH
             "${STAGE2_RPATH_ORIGIN}/${STAGE2_RPATH_ORIGIN_EXTERNAL_TOOLCHAIN}/lib64")
      endif()
    endif()
    if(EXISTS "${BOOTSTRAP_CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}/lib")
      list(APPEND BOOTSTRAP_CMAKE_INSTALL_RPATH "${BOOTSTRAP_CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}/lib")
      if(STAGE2_RPATH_ORIGIN_EXTERNAL_TOOLCHAIN)
        list(APPEND BOOTSTRAP_CMAKE_INSTALL_RPATH
             "${STAGE2_RPATH_ORIGIN}/${STAGE2_RPATH_ORIGIN_EXTERNAL_TOOLCHAIN}/lib")
      endif()
    endif()
    set(BOOTSTRAP_CMAKE_INSTALL_RPATH ${BOOTSTRAP_CMAKE_INSTALL_RPATH} CACHE STRING "" FORCE)
    message("Stage1: Reset BOOTSTRAP_CMAKE_INSTALL_RPATH=${BOOTSTRAP_CMAKE_INSTALL_RPATH}")
  endif()
  set(BOOTSTRAP_CMAKE_SHARED_LINKER_FLAGS "-ldl -lpthread" CACHE STRING "")
  set(BOOTSTRAP_CMAKE_MODULE_LINKER_FLAGS "-ldl -lpthread" CACHE STRING "")
  set(BOOTSTRAP_CMAKE_EXE_LINKER_FLAGS "-ldl -lpthread" CACHE STRING "")
endif()

set(BOOTSTRAP_LLVM_ENABLE_LLD ON CACHE BOOL "")
set(BOOTSTRAP_LLVM_ENABLE_LTO ON CACHE BOOL "")
#[[
# https://github.com/llvm/llvm-project/issues/57545
# ld.gold: internal error in do_layout
]]
set(BOOTSTRAP_LLVM_LLVM_INCLUDE_TESTS OFF CACHE BOOL "")
set(BOOTSTRAP_BOLT_INCLUDE_TESTS OFF CACHE BOOL "")

set(CLANG_BOOTSTRAP_TARGETS
    check-all
    check-clang
    check-lld
    check-llvm
    check-polly
    llvm-config
    clang-test-depends
    lld-test-depends
    llvm-test-depends
    test-suite
    test-depends
    distribution
    install-distribution
    install-distribution-stripped
    install-distribution-toolchain
    clang
    CACHE STRING "")

get_cmake_property(variableNames VARIABLES)
foreach(variableName ${variableNames})
  if(variableName MATCHES "^STAGE2_")
    string(REPLACE "STAGE2_" "" new_name ${variableName})
    list(APPEND EXTRA_ARGS "-D${new_name}=${${variableName}}")
  endif()
endforeach()

# Setup the bootstrap build.
set(CLANG_ENABLE_BOOTSTRAP ON CACHE BOOL "")
set(CLANG_BOOTSTRAP_EXTRA_DEPS builtins runtimes CACHE STRING "")

if(STAGE2_CACHE_FILE)
  set(CLANG_BOOTSTRAP_CMAKE_ARGS -C ${STAGE2_CACHE_FILE} CACHE STRING "")
else()
  set(CLANG_BOOTSTRAP_CMAKE_ARGS -C ${CMAKE_CURRENT_LIST_DIR}/distribution-stage2.cmake CACHE STRING "")
endif()
